package dataStructure.xmg1.a06;

import dataStructure.other.BinarySearch;
import dataStructure.xmg1.a06.printer.BinaryTrees;

import java.util.Comparator;

/**
 * @author: vince_zww
 * @date: 2020/12/13 23:04
 * @desceription:
 */
public class Main {
    private static class PersonComparator implements Comparator<Person> {
        @Override
        public int compare(Person e1, Person e2) {
            return e1.getAge() - e2.getAge();
        }
    }

    private static class PersonComparator2 implements Comparator<Person> {
        @Override
        public int compare(Person e1, Person e2) {
            return e2.getAge() - e1.getAge();
        }
    }

    static void test1() {
        Integer data[] = new Integer[]{
                7,4,9,2,5,8,11,3,12,1
        };

        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        for (int i = 0; i < data.length; i++) {
            bst.add(data[i]);
        }

        BinaryTrees.print(bst, BinaryTrees.PrintStyle.INORDER);
    }

    static void test2() {

        Integer data[] = new Integer[]{
                7,4,9,2,5,8,11,3,12,1
        };
        BinarySearchTree<Person> bst1 = new BinarySearchTree<>();
        for (int i = 0; i < data.length; i++) {
            bst1.add(new Person(data[i]));
        }
        BinaryTrees.println(bst1);

//        System.out.println();
        BinarySearchTree<Person> bst2 = new BinarySearchTree<>(new Comparator<Person>() {
            public int compare(Person o1, Person o2) {
                return o2.getAge() - o1.getAge();
            }
        });
        for (int i = 0; i < data.length; i++) {
            bst2.add(new Person(data[i]));
        }
        BinaryTrees.println(bst2);
    }


    static void test3(){
        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        for (int i = 0; i < 30; i++) {
            bst.add((int) (Math.random() * 100));
        }
        BinaryTrees.print(bst);
    }

    static void test5(){
        BinarySearchTree<Person> bst = new BinarySearchTree<>();
        bst.add(new Person(10,"jack"));
        bst.add(new Person(12,"rose"));
        bst.add(new Person(6,"jim"));
        bst.add(new Person(10,"michael"));

        BinaryTrees.println(bst);
    }

    static void test6() {
        Integer data[] = new Integer[]{
                7,4,9,2,5,8,11,3,12,1,10
        };

        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        for (int i = 0; i < data.length; i++) {
            bst.add(data[i]);
        }

        BinaryTrees.println(bst);

//        bst.levelOrderTraversal();
        //自定义处理逻辑
//        bst.levelOrder(new BinarySearchTree.Visitor<Integer>() {
//            public void visit(Integer element) {
//                System.out.print("_"+element+"_ ");
//            }
//        });
        System.out.println(bst);
//        System.out.println(bst.height());

    }

    //计算二叉搜索树的高度
    static void test7() {
        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        for (int i = 0; i < 20; i++) {
            bst.add((int) (Math.random() * 100));
        }
        BinaryTrees.println(bst);
        System.out.println(bst.height());
    }

    //判断是否完全二叉树
    static void test8() {
        Integer data[] = new Integer[]{
                7,4,9,2,5,1
        };

        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        for (int i = 0; i < data.length; i++) {
            bst.add(data[i]);
        }
//        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
//        for (int i = 0; i < 3; i++) {
//            bst.add((int) (Math.random() * 100));
//        }
        BinaryTrees.println(bst);
        System.out.println(bst.isCompare());
    }

    static void test9() {
        Integer data[] = new Integer[]{
                7,4,9,2,5,8,11,3,12,1
        };

        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        for (int i = 0; i < data.length; i++) {
            bst.add(data[i]);
        }
        //打印
        BinaryTrees.println(bst);

        bst.remove(7);


        //打印
        BinaryTrees.println(bst);

    }

    static void test10(){
        Integer data[] = new Integer[]{
                7,4,9,2,5,8,11,3,12,1
        };

        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        for (int i = 0; i < data.length; i++) {
            bst.add(data[i]);
        }
        //打印
        BinaryTrees.println(bst);

        bst.preorder(new BinarySearchTree.Visitor<Integer>() {
            public boolean visit(Integer element) {
                System.out.print(element+" ");
                return element == 2 ? true : false;
            }
        });
        System.out.println();

        bst.inorder(new BinarySearchTree.Visitor<Integer>() {
            @Override
            public boolean visit(Integer element) {
                System.out.print(element+" ");
                return element == 3 ? true : false;
            }
        });
        System.out.println();

        bst.postorder(new BinarySearchTree.Visitor<Integer>() {
            @Override
            public boolean visit(Integer element) {
                System.out.print(element+" ");
                return element == 2 ? true : false;
            }
        });
        System.out.println();

        bst.levelOrder(new BinarySearchTree.Visitor<Integer>() {
            @Override
            public boolean visit(Integer element) {
                System.out.print(element+" ");
                return element == 9 ? true : false;
            }
        });
        System.out.println();
    }

    public static void main(String[] args) {


        test10();


        /**
         * Java的匿名类，类似于IOS中的Block、JS中的闭包(function)
         */
//        BinarySearchTree<Person> bst1 = new BinarySearchTree<>(new Comparator() {
//            public int compare(Object o1, Object o2) {
//                return 0;
//            }
//        });
//        bst1.add(new Person(12));
//        bst1.add(new Person(15));
//
//        BinarySearchTree<Person> bst2 = new BinarySearchTree<>(new PersonComparator2());
//        bst2.add(new Person(12));
//        bst2.add(new Person(15));
//
//        BinarySearchTree<Person> bst3 = new BinarySearchTree<>(new PersonComparator2());
//        bst3.add(new Person(12));
//        bst3.add(new Person(15));

    }
}
